#include <iostream>
#include <semaphore.h>
#include <pthread.h>

//信号量
namespace SemMoudle
{
    const int defaultvalue = 1;
    class Sem
    {
    public:
        Sem(unsigned int sem_value = defaultvalue)
        {
            sem_init(&_sem, 0, sem_value);
        }

        void P()
        {
            //等待信号量，会将信号量的值减1
            int n = sem_wait(&_sem);//原子的
            (void)n;
        }

        void V()
        {
            //发布信号量，释放资源，会将信号量的值加1
            int n = sem_post(&_sem);//原子的
            (void)n;
        }

        ~Sem()
        {
            sem_destroy(&_sem);
        }

    private:
        sem_t _sem;
    };
}